package com.jianbing.north.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.jianbing.common.utils.SecurityUtils;
import com.jianbing.north.domain.NorthDayPlan;
import com.jianbing.north.service.INorthDayPlanService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jianbing.common.annotation.Log;
import com.jianbing.common.core.controller.BaseController;
import com.jianbing.common.core.domain.AjaxResult;
import com.jianbing.common.enums.BusinessType;
import com.jianbing.north.domain.NorthUserInfo;
import com.jianbing.north.service.INorthUserInfoService;
import com.jianbing.common.utils.poi.ExcelUtil;
import com.jianbing.common.core.page.TableDataInfo;

/**
 * 用户挑战进度表Controller
 * 
 * @author ruoyi
 * @date 2024-06-20
 */
@RestController
@RequestMapping("/north/process")
public class NorthUserInfoController extends BaseController
{
    @Autowired
    private INorthUserInfoService northUserInfoService;
    @Autowired
    private INorthDayPlanService northDayPlanService;
    /**
     * 查询用户挑战进度表列表
     */
    // @PreAuthorize("@ss.hasPermi('north:process:list')")
    @GetMapping("/list")
    public TableDataInfo list(NorthUserInfo northUserInfo)
    {
        startPage();
        List<NorthUserInfo> list = northUserInfoService.selectNorthUserInfoList(northUserInfo);
        return getDataTable(list);
    }

    /**
     * 导出用户挑战进度表列表
     */
    //  @PreAuthorize("@ss.hasPermi('north:process:export')")
    @Log(title = "用户挑战进度表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NorthUserInfo northUserInfo)
    {
        List<NorthUserInfo> list = northUserInfoService.selectNorthUserInfoList(northUserInfo);
        ExcelUtil<NorthUserInfo> util = new ExcelUtil<NorthUserInfo>(NorthUserInfo.class);
        util.exportExcel(response, list, "用户挑战进度表数据");
    }

    /**
     * 获取用户挑战进度表详细信息
     */
    // @PreAuthorize("@ss.hasPermi('north:process:query')")
    @GetMapping()
    public AjaxResult getInfo()
    {
        Long userId = SecurityUtils.getUserId();
        NorthUserInfo northUserInfo  = northUserInfoService.selectNorthUserInfoByUserId(userId);
        if(northUserInfo==null){
            NorthUserInfo northUserInfo1 = new NorthUserInfo();
            northUserInfo1.setUserId(userId);
            northUserInfoService.insertNorthUserInfo(northUserInfo1);
            northUserInfo  = northUserInfoService.selectNorthUserInfoByUserId(userId);
        }
        NorthDayPlan northDayPlan = northDayPlanService.selectNorthDayPlanById(northUserInfo.getDay());
        northDayPlan.setPart2(Math.min(northDayPlan.getPart2(),northUserInfo.getPart2()));
        northDayPlan.setPart2(northDayPlan.getPart2()==0?0:northUserInfo.getPart2());
        return success(northDayPlan);
    }

    /**
     * 新增用户挑战进度表
     */
    // @PreAuthorize("@ss.hasPermi('north:process:add')")
    @Log(title = "用户挑战进度表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NorthUserInfo northUserInfo)
    {
        return toAjax(northUserInfoService.insertNorthUserInfo(northUserInfo));
    }

    /**
     * 修改用户挑战进度表
     */
    //  @PreAuthorize("@ss.hasPermi('north:process:edit')")
    @Log(title = "用户挑战进度表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NorthUserInfo northUserInfo)
    {
        return toAjax(northUserInfoService.updateNorthUserInfo(northUserInfo));
    }

    /**
     * 删除用户挑战进度表
     */
    // @PreAuthorize("@ss.hasPermi('north:process:remove')")
    @Log(title = "用户挑战进度表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        return toAjax(northUserInfoService.deleteNorthUserInfoByUserIds(userIds));
    }


}
